home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Scribefire-1.4.2 / scribefire-1.4.2-fx+fl.xpi / chrome / content / performancingReposition.js < prev    next >
Encoding:
JavaScript  |  2007-02-24  |  12.5 KB  |  344 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is FoxyTunes Mozilla Extension and Engine.
  15.  * The Initial Developer of the Original Code is Alex Sirota <alex@elbrus.com>. 
  16.  * Portions created by Alex Sirota are Copyright (C) 2004-2006 Alex Sirota. All Rights Reserved.
  17.  *
  18.  * Alternatively, the contents of this file may be used under the terms of
  19.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  20.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  21.  * in which case the provisions of the GPL or the LGPL are applicable instead
  22.  * of those above. If you wish to allow use of your version of this file only
  23.  * under the terms of either the GPL or the LGPL, and not to allow others to
  24.  * use your version of this file under the terms of the MPL, indicate your
  25.  * decision by deleting the provisions above and replace them with the notice
  26.  * and other provisions required by the GPL or the LGPL. If you do not delete
  27.  * the provisions above, a recipient may use your version of this file under
  28.  * the terms of any one of the MPL, the GPL or the LGPL.
  29.  *
  30.  * ***** END LICENSE BLOCK ***** */
  31.  
  32.  /*
  33.  * This file's contents (performancingReposition.js) have been based entirely on code by Alex Sirota for the Foxy Tunes Extension, with permission, and in accordance to it's respective licenses.
  34.  * See http://www.foxytunes.com/ for more information.
  35.  */
  36.  
  37. function performancingInstallDragDropObserversForElementById(elementId, bInstall) {
  38.    var elem = document.getElementById(elementId);
  39.    if (elem == null) {
  40.     //gperFormancingUI.printLog("\nAdding observer, cannot find element: " + elementId + "\n");
  41.     return;
  42.    }
  43.  
  44.    if (bInstall) {
  45.     elem.addEventListener("dragover", performancingOnTargetDragOver, false);
  46.     elem.addEventListener("dragexit", performancingOnTargetDragExit, false);
  47.     elem.addEventListener("dragdrop", performancingOnTargetDragDrop, false);
  48.    } else {
  49.     elem.removeEventListener("dragover", performancingOnTargetDragOver, false);
  50.     elem.removeEventListener("dragexit", performancingOnTargetDragExit, false);
  51.     elem.removeEventListener("dragdrop", performancingOnTargetDragDrop, false);
  52.    }
  53. }
  54.  
  55. function performancingInstallUninstallDragDropObservers(bInstall) {
  56.   performancingInstallDragDropObserversForElementById('status-bar', bInstall);
  57.  
  58.   // all the toolboxes:
  59.   var toolboxes = document.getElementsByTagName('toolbox');
  60.   for (var i = 0; i < toolboxes.length; i++) {
  61.     performancingInstallDragDropObserversForElementById(toolboxes[i].id, bInstall);
  62.   }
  63.  
  64.   // performancingInstallDragDropObserversForElementById('navigator-toolbox', bInstall);
  65. }
  66.  
  67. function performancingInstallDragDropObservers() {
  68.    performancingInstallUninstallDragDropObservers(true);
  69. }
  70.  
  71. function performancingUnInstallDragDropObservers() {
  72.    performancingInstallUninstallDragDropObservers(false);
  73. }
  74.  
  75.  
  76. function performancingHasDropClass(className) {
  77.   var classNames = className.split(" ");
  78.   for (var i = 0; i < classNames.length; i++) {
  79.     if (classNames[i].indexOf('performancing-drop-target-') != -1) {
  80.         return true;
  81.     }
  82.   }
  83.   return false;
  84. }
  85.  
  86. function performancingRemoveDropClass(className) {
  87.   var classNames = className.split(" ");
  88.  
  89.   if (classNames.length < 1) {
  90.     return className;
  91.   }
  92.  
  93.   // see if the last class is performancing drop class:
  94.   if (classNames[classNames.length - 1].indexOf('performancing-drop-target-') != -1) {
  95.     classNames.length--; // truncate the array
  96.         if (classNames.length > 0) {
  97.         className = classNames.join(" ");
  98.     } else {
  99.         className = "";
  100.     }
  101.   } 
  102.   return className;
  103. }
  104.  
  105. function performancingElementIsToolbarOrStatusbar(elem) {
  106.   return (elem.localName == "toolbar") || (elem.localName == "statusbar") || (elem.localName == "menubar");
  107. }
  108.  
  109. function performancingSetDropTargetMarker(node, bSet) {
  110.   var target = node;
  111.   var side = 'left';
  112.   if (performancingElementIsToolbarOrStatusbar(node)) {
  113.     target = node.lastChild;
  114.     side = "right";
  115.   }
  116.   
  117.   if (target == null) {
  118.     return;
  119.   }
  120.  
  121.   if (bSet) {
  122.     if (!performancingHasDropClass(target.className)) {
  123.       var dropClassName = 'performancing-drop-target-' + side;
  124.       target.className = target.className + " " + dropClassName;
  125.     }
  126.   } else {
  127.     target.className = performancingRemoveDropClass(target.className);
  128.   } 
  129. }
  130.  
  131.  
  132. function performancingOnTargetDragOver(event)
  133. {
  134.   nsDragAndDrop.dragOver(event, perFormancingTargetObserver);
  135. }
  136.  
  137. function performancingOnTargetDragExit(event)
  138. {
  139.    if (gPerFormancingCurrentDropTarget != null) {
  140.        performancingSetDropTargetMarker(gPerFormancingCurrentDropTarget, false);
  141.    }
  142. }
  143.  
  144. function performancingOnTargetDragDrop(event)
  145. {
  146.   nsDragAndDrop.drop(event, perFormancingTargetObserver);
  147. }
  148.  
  149. var perFormancingDragStartObserver =
  150. {
  151.   onDragStart: function (event, transferData, action) {
  152.     performancingInstallDragDropObservers();
  153.     transferData.data = new TransferData();
  154.     transferData.data.addDataForFlavour('id/performancing-widget', 'performancing-statusbar-panel');
  155.   }
  156. }
  157.  
  158.  
  159. var perFormancingTargetObserver =
  160. {
  161.   onDragOver: function (event, flavour, session)
  162.   {
  163.    //gperFormancingUI.printLog("over\n");
  164.    var topElement = event.target;
  165.    var target = event.target;
  166.    while (topElement && !performancingElementIsToolbarOrStatusbar(topElement)) {
  167.           target = topElement;
  168.           topElement = topElement.parentNode;
  169.    } 
  170.    
  171.    var previousDragItem = gPerFormancingCurrentDropTarget;
  172.  
  173.    if (performancingElementIsToolbarOrStatusbar(target)) {
  174.      gPerFormancingCurrentDropTarget = target;
  175.    } else {
  176.          var targetWidth = target.boxObject.width;
  177.          var targetX = target.boxObject.x;
  178.  
  179.          gPerFormancingCurrentDropTarget = null;
  180.          if (event.clientX > (targetX + (targetWidth / 2))) {
  181.             gPerFormancingCurrentDropTarget = target.nextSibling;
  182.             if (gPerFormancingCurrentDropTarget == null) {
  183.               // last element in its parent, set target to parent
  184.                   gPerFormancingCurrentDropTarget = topElement;
  185.         }
  186.          } else {
  187.             gPerFormancingCurrentDropTarget = target;
  188.          }    
  189.    }
  190.  
  191.  //  gperFormancingUI.printLog("\nprev: " + previousDragItem.id + ", next: " + gPerFormancingCurrentDropTarget.id + "\n");
  192.    if (previousDragItem && (gPerFormancingCurrentDropTarget != previousDragItem)) {
  193.      performancingSetDropTargetMarker(previousDragItem, false);
  194.    }
  195.  
  196.    if (gPerFormancingCurrentDropTarget.id.indexOf('performancing') == -1) { 
  197.        performancingSetDropTargetMarker(gPerFormancingCurrentDropTarget, true);
  198.        session.canDrop = true;
  199.    } else {
  200.      // cannot drop on myself: 
  201.        performancingSetDropTargetMarker(gPerFormancingCurrentDropTarget, false);
  202.        gPerFormancingCurrentDropTarget = null;
  203.        session.canDrop = false;
  204.    }
  205.   },
  206.  
  207.   onDragExit: function (event, session) 
  208.   {
  209.     //gperFormancingUI.printLog("On Performancing Drag Exit");
  210.   },
  211.  
  212.   onDrop: function (event, dropData, session)
  213.   {
  214.     //gperFormancingUI.printLog("On Performancing Drag Drop");
  215.     performancingUnInstallDragDropObservers();
  216.     if (gPerFormancingCurrentDropTarget == null) {
  217.       return; 
  218.     }
  219.     performancingSetDropTargetMarker(gPerFormancingCurrentDropTarget, false);
  220.     
  221.     var draggedItemId = dropData.data;
  222.     // sanity, should never happen:
  223.     if (gPerFormancingCurrentDropTarget.id == draggedItemId) {
  224.       return;
  225.     }
  226.   
  227.     var topElement = event.target;
  228.     while (topElement && !performancingElementIsToolbarOrStatusbar(topElement)) {
  229.       topElement = topElement.parentNode;
  230.     }
  231.   
  232.     // save the new settings:
  233.     gPerFormancingParentElementID = topElement.id;
  234.     gPerFormancingInsertBeforeElementId = gPerFormancingCurrentDropTarget.id;
  235.   
  236.     // for the case when the "insert before" element is a dynamic one, remember the 
  237.     // "insert after" element
  238.     if (gPerFormancingCurrentDropTarget.previousSibling) {    
  239.       gPerFormancingInsertAfterElementId = gPerFormancingCurrentDropTarget.previousSibling.id;
  240.     }
  241.     
  242.     gperformancing.savePositionPrefs();//Save Prefs
  243.     
  244.     performancingSetPerFormancingPosition();
  245.   
  246.     //gperFormancingUI.printLog("Inserted to: " + topElement.id + ", before " + gPerFormancingCurrentDropTarget.id);
  247.     gPerFormancingCurrentDropTarget = null;
  248.   },
  249.   
  250.   
  251.   getSupportedFlavours: function ()
  252.   {
  253.     var flavours = new FlavourSet();
  254.     flavours.appendFlavour("id/performancing-widget");
  255.     return flavours;
  256.   }
  257. }
  258.  
  259.  
  260. function performancingRenameTagName(elem, newTagName) {
  261.     var newElem = document.createElement(newTagName);
  262.     
  263.     // copy all the attributes of the element
  264.     for (var i=0; i < elem.attributes.length; i++) {
  265.             newElem.setAttribute(elem.attributes[i].nodeName, elem.attributes[i].nodeValue);
  266.     }
  267.     
  268.     // move all the children
  269.     var children = elem.childNodes;
  270.     for (var i=children.length-1; i >=0 ; i--) {
  271.     var currentNode = children[i];
  272.             elem.removeChild(currentNode);
  273.             newElem.insertBefore(currentNode, newElem.firstChild);
  274.     }
  275.     return newElem;
  276. }
  277.  
  278.  
  279. function performancingSetPerFormancingPosition() {
  280.   if ((gPerFormancingParentElementID == '') || (gPerFormancingInsertBeforeElementId == '')) {
  281.   // nothing's set, return
  282.   return;
  283.   }
  284.  
  285.     var performancingWidget = document.getElementById('performancing-statusbar-panel');
  286.  
  287.     var parentElement = document.getElementById(gPerFormancingParentElementID);
  288.     if (parentElement == null) {
  289.         return;
  290.     }
  291.     //gperFormancingUI.printLog("\nParent element: " + gPerFormancingParentElementID + "\n");
  292.  
  293.     var insertBeforeElement = document.getElementById(gPerFormancingInsertBeforeElementId);
  294.     var insertAfterElement = document.getElementById(gPerFormancingInsertAfterElementId);
  295.  
  296.     if ((insertBeforeElement == null) && (insertAfterElement == null)) {
  297.         return;
  298.     }
  299.  
  300.     //gperFormancingUI.printLog("\nInsert before element: " + gPerFormancingInsertBeforeElementId + "\n");
  301.     //gperFormancingUI.printLog("\nInsert after element: " + gPerFormancingInsertAfterElementId + "\n");
  302.  
  303.     var oldParentNode = performancingWidget.parentNode;
  304.  
  305.     performancingWidget.parentNode.removeChild(performancingWidget);
  306.  
  307.     try {
  308.         // make PerFormancing toolbaritem and not statubarpanel if needed:
  309.         if ((parentElement.localName == 'toolbar') && (performancingWidget.localName == 'statusbarpanel')) {
  310.             performancingWidget = performancingRenameTagName(performancingWidget, 'toolbaritem');
  311.         }
  312.  
  313.         // make PerFormancing statusbarpanel and not toolbaritem if needed:
  314.         if ((parentElement.localName == 'statusbar') && (performancingWidget.localName == 'toolbaritem')) {
  315.             performancingWidget = performancingRenameTagName(performancingWidget, 'statusbarpanel');
  316.         }
  317.  
  318.         // convention, if the parent equals insertbefore, insert as last
  319.         if (parentElement != insertBeforeElement) {
  320.             if (insertBeforeElement) {
  321.                 parentElement.insertBefore(performancingWidget, insertBeforeElement);
  322.             } else {
  323.                 //gperFormancingUI.printLog('insert before failed try inserting after ' + gPerFormancingInsertAfterElementId + '\n');
  324.                 if (insertAfterElement.nextSibling) {
  325.                     parentElement.insertBefore(performancingWidget, insertAfterElement.nextSibling);
  326.                 } else {
  327.                     parentElement.appendChild(performancingWidget);
  328.                 }
  329.             }
  330.         } else {
  331.             parentElement.appendChild(performancingWidget);
  332.         }
  333.  
  334.         // need to re-setup the volume slider listeners:
  335.         //performancingRegisterVolumeSliderEvents();
  336.     } catch (err) {
  337.         //gperFormancingUI.printLog("\nCouldn't reposition PerFormancing: " + err + "\n");
  338.         oldParentNode.appendChild(performancingWidget);
  339.     }
  340. }
  341.  
  342. // the current drop target:
  343. var gPerFormancingCurrentDropTarget = null;
  344.